//#include "StdAfx.h"
#include "FontRenderer.h"

CFontRenderer::CFontRenderer( LPDIRECT3DDEVICE9 pDevice, int iHeight )
{
	m_pDevice = pDevice;

	D3DXCreateSprite( m_pDevice, &m_pTextSprite );
	D3DXCreateFont( m_pDevice, -iHeight, 0, 0, 1, FALSE, DEFAULT_CHARSET, 
					OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, 
					"Times New Roman", &m_pFont );
}

CFontRenderer::~CFontRenderer(void)
{
	SafeRelease( m_pTextSprite );
	SafeRelease( m_pFont );
}

void CFontRenderer::Render( D3DXVECTOR3 pos, string text, bool colorful, D3DXCOLOR color, bool onScreen, D3DXVECTOR2 scrPos )
{
	D3DXCOLOR m_color;
	if ( colorful )
	{
		m_color = color;
	}else
	{
		m_color = D3DXCOLOR ( 1.0f, 1.0f, 1.0f, 1.0f );
	}

	if (!onScreen)
	{
		D3DXMATRIX sm;
		D3DXMatrixScaling( &sm, 0.1f, -0.1f, 0.1f );
		m_pTextSprite->SetTransform( &sm );

		D3DXMATRIX mm, vm;
		D3DXMatrixTranslation( &mm, pos.x, pos.y, pos.z );
		m_pDevice->GetTransform( D3DTS_VIEW, &vm );
		m_pTextSprite->SetWorldViewLH( &mm, &vm );

		m_pTextSprite->Begin( D3DXSPRITE_ALPHABLEND | D3DXSPRITE_BILLBOARD );

		SetRect( &m_rc, 0, 0, 0, 0 );   
		m_pFont->DrawText( m_pTextSprite, text.c_str(), text.length(), &m_rc, DT_NOCLIP|DT_EXPANDTABS|DT_CENTER, m_color );

		m_pTextSprite->End();

	}else{

		SetRect( &m_rc, static_cast<int >(scrPos.x), static_cast<int >(scrPos.y), 0, 0 );
		m_pFont->DrawText( NULL, text.c_str(), text.length(), &m_rc, DT_NOCLIP|DT_EXPANDTABS, m_color );

	}
}